﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Chess.Common.Game
{
    public enum PieceType
    {
        Pawn,
        Rook,
        Knight,
        Bishop,
        Queen,
        King
    }

    public static class PieceTypeExtension
    {
        public static List<char> GetFiles(this PieceType type)
        {
            switch (type)
            {
                case PieceType.Pawn:
                    return new List<char> { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
                case PieceType.Rook:
                    return new List<char> { 'A', 'H' };
                case PieceType.Knight:
                    return new List<char> { 'B', 'G' };
                case PieceType.Bishop:
                    return new List<char> { 'C', 'F' };
                case PieceType.Queen:
                    return new List<char> { 'D' };
                case PieceType.King:
                    return new List<char> { 'E' };
            }

            throw new ArgumentException("Invalid chess piece: how did this happen?");
        }

        public static int GetRank(this PieceType type)
        {
            return type == PieceType.Pawn ? 1 : 0;
        }

        public static List<Coordinate> GetPossibleMoves(this PieceType type, Coordinate coordinate)
        {
            switch (type)
            {
                // fill out the rest later
                case PieceType.Pawn:
                    return GetPawnMoves(coordinate);
                case PieceType.Rook:
                    return GetRookMoves(coordinate);
                case PieceType.Knight:
                    return null;
                case PieceType.Bishop:
                    return null;
                case PieceType.Queen:
                    return null;
                case PieceType.King:
                    return null;
            }

            throw new ArgumentException("Invalid chess piece: how did this happen?");
        }

        private static List<Coordinate> GetPawnMoves(Coordinate coordinate)
        {
            return null;
        }

        private static List<Coordinate> GetRookMoves(Coordinate coordinate)
        {
//            return Coordinate.Files.Select(file => new Coordinate(file, coordinate.Rank))
//                .Union(
//                   Coordinate.Ranks.Select(rank => new Coordinate(coordinate.File, rank)))
//                .Except(new List<Coordinate> { coordinate }).ToList();

            List<Coordinate> moves = new List<Coordinate>();

            for (char file = 'A'; file <= 'H'; file++)
            {
                moves.Add(new Coordinate(file, coordinate.Rank));
            }

            for (char rank = '1'; rank <= '8'; rank++)
            {
                moves.Add(new Coordinate(coordinate.File, rank));
            }

            moves.Remove(coordinate);
            moves.Remove(coordinate);

            return moves;
        }
    }
}
